package cc.autoapi.pucong.auto.flow.core.flow.execNode.utils;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.exception.AutoDesignException;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.DataTypeEnum;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: cong.pu
 * @create: 2024-09-30 09:54
 **/
@Slf4j
public class DataValueUtils {

    public static Object transDataType(String paramCode, DataTypeEnum dateType, Object dateValue) {
        if (dateValue == null) {
            return null;
        }
        try {
            if (dateType == DataTypeEnum.STRING || dateType == DataTypeEnum.TXT) {
                if (dateValue instanceof String ||
                        dateValue instanceof Integer ||
                        dateValue instanceof Boolean ||
                        dateValue instanceof Long) {
                    return dateValue.toString();
                } else {
                    return JSON.toJSONString(dateValue);
                }
            } else if (dateType == DataTypeEnum.NUMBER) {
                if (dateValue instanceof Boolean) {
                    return (Boolean) dateValue ? 1 : 0;
                } else if (dateValue instanceof String) {
                    try {
                        return Integer.parseInt(dateValue.toString());
                    } catch (Exception e) {
                        if (dateValue.equals("true")) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                } else if (dateValue instanceof BigDecimal) {
                    return dateValue;
                } else if (dateValue instanceof Double) {
                    return dateValue;
                } else if (dateValue instanceof Float) {
                    return dateValue;
                } else if (dateValue instanceof Integer) {
                    return dateValue;
                } else {
                    return Integer.parseInt(dateValue.toString());
                }
            } else if (dateType == DataTypeEnum.LIST) {
                return JSON.parseArray(dateValue.toString());
            } else if (dateType == DataTypeEnum.OBJECT) {
                return dateValue;
            } else if (dateType == DataTypeEnum.BOOLEAN) {
                Boolean boolValue = true;
                if (dateValue instanceof String) {
                    if (StringUtils.isBlank(dateValue.toString())) {
                        boolValue = false;
                    } else if ("true".equals(dateValue) || "1".equals(dateValue) || "是".equals(dateValue) || "正确".equals(dateValue)) {
                        boolValue = true;
                    }
                } else if (dateValue instanceof Boolean) {
                    boolValue = (Boolean) dateValue;
                } else if (dateValue instanceof Integer) {
                    if (((Integer) dateValue) > 0) {
                        boolValue = true;
                    }
                } else if (dateValue instanceof List) {
                    if (((List) dateValue).size() > 0) {
                        boolValue = true;
                    }
                }
                if (dateValue == null) {
                    boolValue = false;
                }
                return boolValue;
            } else if (dateType == DataTypeEnum.MAP) {
                JSONObject jsonObject = new JSONObject();
                if (dateValue instanceof String) {
                    String string = dateValue.toString();
                    try {
                        jsonObject = JSON.parseObject(string);
                    } catch (Exception e) {
                        jsonObject.put("value", string);
                    }
                } else if (dateValue instanceof JSONObject) {
                    jsonObject = (JSONObject) dateValue;
                } else if (dateValue instanceof Map) {
                    Map map = (Map) dateValue;
                    jsonObject = JSONObject.parseObject(JSON.toJSONString(map));
                }
                return jsonObject;
            }
        } catch (Exception e) {
            log.error("数据类型转换异常", e);
            throw new AutoDesignException(String.format("节点参数值转换错误，参数编码：%s,数据类型转换失败: %s -> %s", paramCode, dateType.name(), dateValue));
        }
        return dateValue;
    }
}
